module tag_ui

// Display system tags
define page tags()
{main(tagswiki){
  list {
    for(tag: Tag order by tag.popularity desc) {
      listitem {
        tags_listtag(tag)
      }
    }
  }
}}

define template tags_listtag(tg: Tag)
{
  var princtgentry: TagEntry := null
  init {
    if(securityContext.loggedIn) {
      princtgentry := tg.tgentries_fromuser(securityContext.principal);
    }
  }
  
  listtag(tg, princtgentry)
}

define template listtag(tg: Tag, tgentry: TagEntry)
{
  navigate(tag_references(tg)){output(tg.name)}
  if(tgentry != null) {
    ", "
    navigate(homepage(tgentry.user, false, "")){ output(tgentry.user.name) }
    ": "
    navigate(tagentry_references(tgentry)){ output(tgentry.popularity) }
  }
}

// Display what users have used this tag 
define page tag_references(tg: Tag)
{main(tag_referenceswiki){
  list {
    for(tgentry: TagEntry in tg.tgentries order by tgentry.popularity desc) {
      listitem {
        listtag(tg, tgentry)
      }
    }
  }
}}